package gupao.concurrency.threads;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ThreadYieldDemo2 {
    static long max = Long.MAX_VALUE - 1;

    static class YieldRunnable implements Runnable{
        long count = 0;
        @Override
        public void run() {
            for(long i = 0; i < max; i++){
                Thread.yield();
                count++;
                if(count % 1000000000 == 0){
                    log.info("yield thread count: {}", count);
                }
            }
        }
    }

    static class NotYieldRunnable implements Runnable{
        long count = 0;
        @Override
        public void run() {
            for(long i = 0; i < max; i++){
                count++;
                if(count % 1000000000 == 0){
                    log.info("Not yield thread Count: {}", count);
                }
            }
        }
    }

    public static void main(String[] args){
        YieldRunnable yr = new YieldRunnable();
        Thread yieldThread = new Thread(yr);
        yieldThread.setDaemon(true);

        NotYieldRunnable nyr = new NotYieldRunnable();
        Thread notYieldThread = new Thread(nyr);
        notYieldThread.setDaemon(true);

        yieldThread.start();
        notYieldThread.start();

        Utils.sleepIgnoreExp(5000);
        log.info("Completed.Yield Thread Count: {}, Not yield thread count: {}", yr.count, nyr.count);
    }
}
